package cn.jingzhuan.element.demo.view

import android.content.Context
import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.v7.app.ActionBar
import android.support.v7.widget.Toolbar
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

abstract class BaseBindingFragment<T:ViewDataBinding> : BaseFragment() {

    lateinit var binding: T
    private var mAfterDataBindListener: OnAfterDataBindListener? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val rootView = inflater.inflate(getLayoutId(), container, false)
        binding = createDataBinding(rootView)
        onDataBind(savedInstanceState, binding)
        if (mAfterDataBindListener != null) {
            mAfterDataBindListener?.onDataBind(context!!)
        }
        return rootView
    }

    @LayoutRes
    protected abstract fun getLayoutId(): Int

    protected abstract fun onDataBind(savedInstanceState: Bundle?, binding: T)

    fun createDataBinding(rootView: View): T {
        return DataBindingUtil.bind(rootView)!!
    }

    interface OnAfterDataBindListener {
        fun onDataBind(context: Context)
    }

    fun setAfterDataBindListener(afterDataBindListener: OnAfterDataBindListener) {
        this.mAfterDataBindListener = afterDataBindListener
    }

    fun setActionbar(toolbar: Toolbar) {
        getBaseActivity().setSupportActionBar(toolbar)
    }

    fun getActionbar(): ActionBar? {
        return getBaseActivity().supportActionBar
    }



}
